// 全局配置
const globalConfig = {
    companyId: '2' // 诺丁企服
};

const service = axios.create({
	baseURL: 'http://nodines.cn/api',
	// baseURL: 'http://192.168.0.55:18037/api',
	timeout: 1000 * 60 * 60
});

service.interceptors.request.use(
	(config) => {
		config.headers['content-type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
		return config;
	},
	(error) => {
		return Promise.reject(error);
	}
);

service.interceptors.response.use(
	(response) => {
		const res = response.data;
		if (res.code !== 0) {
			return Promise.reject(new Error(res.message || 'Error'));
		} else {
			return res;
		}
	},
	(error) => {
		return Promise.reject(error);
	}
);

const request = (config) => {
	config = Object.assign({ method: 'get', url: '', data: {} }, config)
	// 自动添加全局ID到请求数据中
	const data = config.method.toLowerCase() === 'get' 
		? { ...config.params, companyId: globalConfig.companyId }
		: { ...config.data, companyId: globalConfig.companyId };

	if (['get', 'put', 'delete'].includes(config.method.toLowerCase())) {
		return service({
			url: config.url,
			method: config.method,
			params: data  // 对于 GET 请求使用 params
		})
	} else {
		return service({
			url: config.url,
			method: config.method,
			data: data    // 对于 POST 请求使用 data
		})
	}
}